import EventDispatcher from "./EventDispatcher";

export default class EventProxy {


    private dispatcher: EventDispatcher;
    private eventMap: { [k: string]: any } = {}
    protected count: number = 0;
    setDispatcher(dispatcher: EventDispatcher) {
        this.dispatcher = dispatcher;
        this.count = 0;
    }

    on(eventName: string, func: Function, target: Object) {
       
        this.count++;
        if (!this.eventMap[eventName]) {
            this.dispatcher.on(eventName, func, target);
            this.eventMap[eventName] = { eventName: eventName, target: target, func: func };
        }
    }

    has(eventName: string) {
        return this.eventMap[eventName] != undefined;
    }

    offAll() {
        if (this.dispatcher) {
            for (const key in this.eventMap) {
                const element = this.eventMap[key];
                this.count--;
                this.dispatcher.off(element.eventName, element.func, element.target);

            }
            this.eventMap = {}
        }

    }
}